<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  data_cache.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-06-03.
//  Copyright 2011 lishuzu. All rights reserved.
// 
/**
* 数据缓存类
*/
class Datacache
{
	
	function __construct()
	{
		$this->CI =& get_instance();
		$this->CI->load->model('Goods_model');
		$this->CI->load->helper('cache');
	}
	//品牌缓存
	function brand_cache(){
		$this->CI->db->order_by('firstletter', 'ASC');
		$query = $this->CI->db->get('brand');
		foreach ($query->result_array() as $row) {
			if ($row['logo']) {
				$row['url'] = base_url().'upload/logo/'.$row['logo'];
			}
			$data[$row['bid']] = $row;
		}
		cache_write('brand', "config['brand']", $data);
	}
	//主页缓存
	function index_cache(){
		$this->CI->load->model('Activity_model');
		$data = $this->CI->Activity_model->get_activitys('', '', 1);
		foreach ($data as $key => $activity) {
			$activity['goods'] = $this->CI->Goods_model->get_goods($activity['aid'], 5);
		}
		cache_write('index', "_SGLOBAL['index_data']", $data);		
	}
	//站点全局配置
	function config_cache(){
		$query = $this->CI->db->get('config');
		foreach ($query->result_array() as $key => $row) {
			$config[$row['var']] = $row['datavalue'];
		}
		cache_write('config', "config['site_config']", $config);		
	}
	 // 分类缓存
	function category_cache(){
		$this->CI->db->where('displayorder >=', '0');
		$this->CI->db->order_by('displayorder', "ASC");
		$query = $this->CI->db->get('category');
		foreach ($query->result_array() as $key => $row) {
			$category[$row['catid']] = $row;
		}
		cache_write('category', "config['category']", $category);
	}
	
	// 来源缓存
	function from_cache(){
		$this->CI->db->order_by('fromid', 'ASC');
		$query = $this->CI->db->get('from');
		foreach ($query->result_array() as $row) {
			$data[$row['fromid']] = $row;
		}
		cache_write('fromsource', "config['fromsource']", $data);
	}
	
	// 统计缓存
	function statistics(){
		$data = $where = $cat_static = array();
		$category = $this->CI->config->item('category');
		$data['user_num'] = $this->CI->db->count_all_results('user');
		// 活动总数
		$data['activity_num'] = $this->CI->db->count_all_results('activity');
		// 上架活动数量
		$where = array(
					'endtime >=' => now(),
					'status >=' => 0,
		);
		$this->CI->db->where($where);
		$data['upindex_act'] = $this->CI->db->count_all_results('activity');
		// 下架活动数量
		$data['downindex_act'] =  $data['activity_num'] - $data['upindex_act'];
		// 商品总数
		$data['goods_num'] = $this->CI->db->count_all_results('goods');

		if ($category) {
			foreach ($category as $catid => $cat) {
				// 所属分类总数
				$where = array('catid' => $catid,
				);
				$this->CI->db->where($where);
				// 上架活动未过期
				$total = $this->CI->db->count_all_results('activity');
				$where = array('catid' => $catid,
							'endtime >=' => now(),
							'status >=' => 0,
				);
				$this->CI->db->where($where);
				$upindex_act = $this->CI->db->count_all_results('activity');
				$cat_static[$catid]['total'] = $total;
				$cat_static[$catid]['upindex_act'] = $upindex_act;
				$cat_static[$catid]['downindex_act'] = $total- $upindex_act;
				$cat_static[$catid]['catname'] = $cat['name'];
			}
		}
		//分类统计
		$data['cat_static'] = $cat_static;
		// 点击排行
		$this->CI->db->select('aid, title, jumpnum, replynum');
		$this->CI->db->order_by('jumpnum', 'DESC');
		$this->CI->db->limit(20);
		$query = $this->CI->db->get('activity');
		$data['jumptop50'] = $query->result_array();
		cache_write('statistics', "config['statistics']", $data);
	}
	
	// 来访缓存
	function guest_cache(){
		$data = array();
		$data['count'] = $this->CI->db->count_all_results('guest');
		$data['count'] = 2000 + $data['count'];
		$this->CI->db->limit('52');
		$this->CI->db->order_by('lastlogin', 'DESC');
		$query = $this->CI->db->get('guest');
		foreach ($query->result_array() as $row) {
			$data[$row['guestid']] = $row;
		}
		cache_write('guest', "config['guest']", $data);
	}	
}

?>